package us.du.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import sun.security.krb5.internal.ccache.Credentials;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.downloader.HttpClientDownloader;
import us.codecraft.webmagic.proxy.Proxy;
import us.codecraft.webmagic.proxy.SimpleProxyProvider;
import us.codecraft.webmagic.selector.Html;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * 亚马逊上坡信息采集服务
 *
 * @author zhangcheng
 * @date 2019/10/28 19:44
 */
@Slf4j
@Component
public class AmzonProductCrawlService {

    @Value("${proxy.hostname}")
    private String proxyHost;
    @Value("${proxy.port}")
    private int proxyPort;
    @Value("${proxy.user}")
    private String proxyUser;
    @Value("${proxy.password}")
    private String proxyPass;

    public Map<String, Object> cralw(String asin) {
        HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
        httpClientDownloader.setProxyProvider(SimpleProxyProvider.from(new Proxy(proxyHost, proxyPort, proxyUser, proxyPass)));
        Request request = new Request("https://www.amazon.com/dp/" + asin);
        Page page = httpClientDownloader.download(request, Site.me().toTask());
        Html html = page.getHtml();
        System.out.println(html.xpath("//title/text()"));
        //标题解析
        String title = html.xpath("//*[@id=\"productTitle\"]/text()").get();
        //价格解析
        String price = getPrice(html);
        //主图解析
        String imgPath = html.xpath("//*[@id=\"imgTagWrapperId\"]/img/@data-old-hires").get();
        //一级类目解析
        String category = html.xpath("//*[@id=\"wayfinding-breadcrumbs_feature_div\"]//ul/li[1]//a/text()").get();
        //描述解析
        String description = html.xpath("//*[@id=\"featurebullets_feature_div\"]/tidyText()").get();

        //组装并存入数据到mongodb
        Map<String, Object> map = new HashMap<>();
        map.put("title", title);
        map.put("price", price);
        map.put("imgPath", imgPath);
        map.put("category", category);
        map.put("description", description);
        map.put("crawl_time", DateUtil.now());
        map.put("asin", asin);
        return map;
    }


    /***
     * 价格提取
     * @param html
     * @return
     */
    public static String getPrice(Html html) {
        //price
        String price = html.xpath("//*[@id=\"priceblock_ourprice\"]/text()").get();
        if (StringUtils.isBlank(price)) {
            price = html.xpath("//*[@id=\"priceblock_saleprice\"]/text()").get();
        }
        if (StringUtils.isBlank(price)) {
            price = html.xpath("//*[@id=\"priceblock_dealprice\"]/text()").get();
        }
        if (StringUtils.isBlank(price)) {
            price = html.xpath("//*[@id=\"snsPrice\"]/div/span[2]/text()").get();
        }
        return price;
    }


}
